/* Y o u r   D e s c r i p t i o n                       */
/*                            AppBuilder Photon Code Lib */
/*                                         Version 2.03  */

/* Standard headers */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

/* Local headers */
#include "ablibs.h"
#include "global.h"
#include "abimport.h"
#include "proto.h"


int
Tty_open( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
	PtArg_t arg;

	/* eliminate 'unreferenced' warnings */
	widget = widget, apinfo = apinfo, cbinfo = cbinfo;


	/* Open a pseudo tty -- NULL is a shortcut for "/dev";
	 * the widget will add something like "ttyp3" to it
	 */
	PtSetArg( &arg, Pt_ARG_TTY_PSEUDO, NULL, 0 );
	PtSetResources( ABW_PtTty1, 1, &arg );

	/* Have we succeeded? */
	PtSetArg( &arg, Pt_ARG_TTY_SFD, 0, 0 );
	PtGetResources( ABW_PtTty1, 1, &arg );
	if ( arg.value == -1 )
    	PtTerminalPuts( ABW_PtTty1, "Unable to find a pty\r\n" );
	else {
	    /* Run a program on the pseudo tty.
    	 * NULL is more or less a shortcut for
	     *   "char *argv[] = { "/bin/sh", NULL };",
	     * except it runs *your* shell rather than always /bin/sh.
	     */
    	PtSetArg( &arg, Pt_ARG_TTY_ARGV, NULL, 0 );
	    PtSetResources( ABW_PtTty1, 1, &arg );

	    /* Have we succeeded? */
	    PtSetArg( &arg, Pt_ARG_TTY_PID, NULL, 0 );
	    PtGetResources( ABW_PtTty1, 1, &arg );
	    if ( arg.value == 0 )
	        PtTerminalPuts( ABW_PtTty1, "Unable to spawn the shell\r\n" );
	}

	return( Pt_CONTINUE );

}

